package com.example.demo.server;

import com.example.demo.code.CodeUser;
import com.example.demo.dao.DaoUser;
import com.example.demo.dao.SqlUser;
import com.example.demo.pojo.Root;
import com.example.demo.pojo.WorkUser;
import com.example.demo.util.ExcelUtil;
import com.example.demo.util.FileUtil;
import com.example.demo.util.RootUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.util.List;
import java.util.Objects;

@Service
public class UserServer {
    @Autowired
    DaoUser daoUser;
    @Autowired
    SqlUser sqlUser;
    @Autowired
    private JavaMailSender mailSender;
    @Value("${spring.mail.username}")
    private String from;

    public CodeUser userLogin(String id, String pwd){
        CodeUser codeUser=new CodeUser();
        List<WorkUser> S= daoUser.findAllByIdOrEmail(id,id);
        if(S.isEmpty())
            codeUser.setCode(400);
        else {
            S= sqlUser.getUserByIdAndOrEmail(id,pwd);
            if(S.isEmpty())
                codeUser.setCode(400);
            else {
                codeUser.setCode(200);
                codeUser.setUser(S.get(0));
            }
        }
        return  codeUser;
    }
    public String sendSimpleMail(String to) throws Exception {
        String s=FileUtil.getRandomString(6);
        SimpleMailMessage message = new SimpleMailMessage();
        message.setTo(to);
        message.setSubject("【企业在线学习平台】 邮箱验证码 ");
        message.setText("【企业在线学习平台】 验证码\n"+"您申请的验证码为： "+s);
        message.setFrom(from);
        mailSender.send(message);
        return s;
    }
    public CodeUser userRegister(WorkUser user){
        CodeUser codeUser=new CodeUser();
        List<WorkUser>S=daoUser.findAllByIdOrEmail(user.getId(),user.getEmail());
        if(!S.isEmpty()){
            codeUser.setCode(400);
        }
        else {
            daoUser.save(user);
            codeUser.setCode(200);
        }
        return codeUser;
    }
    public List<Root> getUserRootList(String type)throws Exception{
        if(Objects.equals(type, "admin"))
            return RootUtil.xml2RootList("E:\\代码\\ASP.net Web程序设计\\web程序设大作业\\后端\\myworkserver\\src\\main\\UserInfo\\adminPowerList.xml");

        else
            return RootUtil.xml2RootList("E:\\代码\\ASP.net Web程序设计\\web程序设大作业\\后端\\myworkserver\\src\\main\\UserInfo\\userPowerList.xml");


    }
    public List<WorkUser> getUserList(int index,String s){
            Pageable pageable = PageRequest.of(index, 9);
           return daoUser.findByTypeAndNameContainingOrPetNameContainingOrSexContaining("user",s,s,s,pageable).getContent();
    }
    public int countUser(String key){
        return daoUser.countByTypeAndNameContainingOrPetNameContainingOrSexContaining("user",key,key,key);
    }
    public int uploadUserList(MultipartFile file) {
        int code=200;
        try {
            File f=FileUtil.multipartFile2File(file);
            ExcelUtil excelUtil=new ExcelUtil(f);
            for (int i=1;i<excelUtil.sheet.getPhysicalNumberOfRows();i++) {
                WorkUser workUser = new WorkUser();
                if (Objects.equals(excelUtil.getExcelDateByIndex(i, 0), ""))
                    workUser.setId(FileUtil.getRandomString(6));
                else
                    workUser.setId(excelUtil.getExcelDateByIndex(i, 0));
                workUser.setName(excelUtil.getExcelDateByIndex(i, 1));
                if (Objects.equals(excelUtil.getExcelDateByIndex(i, 2), ""))
                    workUser.setEmail(FileUtil.getRandomString(6));
                else
                    workUser.setEmail(excelUtil.getExcelDateByIndex(i, 2));
                workUser.setPassword(excelUtil.getExcelDateByIndex(i, 3));
                workUser.setTel(excelUtil.getExcelDateByIndex(i, 4));
                workUser.setSex(excelUtil.getExcelDateByIndex(i, 5));
                workUser.setPetName(excelUtil.getExcelDateByIndex(i, 6));
                workUser.setType("user");
                daoUser.save(workUser);
            }
        } catch (Exception exception) {
            code=400;
        }

        return  code;
    }
    public int  deleteUser(String id){
        int code=200;
        try {
            daoUser.deleteById(id);
        }
        catch (Exception e){
            e.printStackTrace();
            code=400;
        }
        return code;
    }
    public int editUser(String id,WorkUser user){
        int code=200;
        if(Objects.equals(id, user.getId())){
            daoUser.save(user);
        }
        else {
            code=deleteUser(id);
            daoUser.save(user);
        }
        return code;
    }
    public int resetPwd(String email,String pwd){
        int code=200;
        if(daoUser.countByEmail(email)==0)
            return code=400;
        else {
            WorkUser workUser=daoUser.findByEmail(email);
            workUser.setPassword(pwd);
            daoUser.save(workUser);
        }
        return code;
    }


}
